package com.atguigu.userprofile.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.userprofile.bean.TagCondition;
import com.atguigu.userprofile.bean.TaskInfo;
import com.atguigu.userprofile.bean.UserGroup;
import com.atguigu.userprofile.service.UserGroupService;
import com.atguigu.userprofile.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2021-05-04
 */
@RestController
public class UserGroupController {

    @Autowired
    UserGroupService userGroupService;

    @RequestMapping("/user-group-list")
    @CrossOrigin
    public String  getUserGroupList(@RequestParam("pageNo")int pageNo , @RequestParam("pageSize") int pageSize){
        int startNo=(  pageNo-1)* pageSize;
        int endNo=startNo+pageSize;

        QueryWrapper<UserGroup> queryWrapper = new QueryWrapper<>();
        int count = userGroupService.count(queryWrapper);

        queryWrapper.orderByDesc("id").last(" limit " + startNo + "," + endNo);
        List<UserGroup> userGroupList = userGroupService.list(queryWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("detail",userGroupList);
        jsonObject.put("total",count);

        return  jsonObject.toJSONString();
    }

    @PostMapping("/user-group")
    public  String createUserGroup(@RequestBody UserGroup userGroup){
        //userGroupService.saveOrUpdate(userGroup);
        //1分群基本信息的保存
        userGroupService.saveUserGroupInfo(userGroup,false);
        //2  通过clickhouse 进行筛选，获得uid 集合(人群包)保存到clickhouse中
        userGroupService.genUserGroup(userGroup);
        //  3  为了下游数据的高频访问，把数据结果再写入redis一份。
        Integer userGroupNum = userGroupService.saveUserGroupToRedis(userGroup);
        // 4 补充 分群人数
        userGroup.setUserGroupNum(userGroupNum+0L);
        userGroupService.saveUserGroupInfo(userGroup,false);

        return "success";

    }

    @PostMapping("/user-group-evaluate")
    public Integer  evaluateUserGroupNum(@RequestBody UserGroup userGroup){
      Integer userGroupNum=  userGroupService.evaluateUserGroupNum(userGroup);
      return  userGroupNum;
    }

    @PostMapping("/user-group-refresh/{id}")
    public String  refreshUserGroup(@PathVariable("id")String userGroupId,@RequestParam("busiDate")String busiDate){
        // 1  从mysql中查询分群定义
        UserGroup userGroup=  userGroupService.getUserGroupInfoById( userGroupId);
        userGroup.setBusiDate(busiDate);
        //2  通过clickhouse 进行筛选，获得uid 集合(人群包)保存到clickhouse中
        userGroupService.genUserGroup(userGroup);
        //3  为了下游数据的高频访问，把数据结果再写入redis一份。
        Integer userGroupNum = userGroupService.saveUserGroupToRedis(userGroup);
        // 4 补充 分群人数
        userGroup.setUserGroupNum(userGroupNum+0L);
        userGroupService.saveUserGroupInfo(userGroup,true);

        return "success";
    }




}

